package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelContent;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.query.UserQueryObject;
import cn.wolfcode.trip.base.service.ITravelContentService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JsonResult;
import com.alibaba.druid.support.json.JSONUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/*
 * 游记资源控制器
 * */
@Controller
@RequestMapping("/travels")
public class TravelController {
    @Autowired
    private ITravelService travelService;
    @Autowired
    private ITravelContentService travelContentService;

    /**
     *  查询全部的游记
     */
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") TravelQueryObject qo) {
        // 待审核状态 1
        if (qo.getState() == null) {
            qo.setState(Travel.STATE_AUDIT);
        }
        // 是否公开
        qo.setIsPublic(true);
        // 最后更新的时间升序排序
        qo.setOrderBy("t.lastUpdateTime asc");
        model.addAttribute("pageInfo", travelService.query(qo));
        return "/travels/list";
    }

    /**
     *  查询已发布游记
     */
    @RequestMapping("/releaseList")
    public String releaseList(Model model, @ModelAttribute("qo") TravelQueryObject qo) {
        // 已经发布的游记
        qo.setState(Travel.STATE_RELEASE);
        // 是否公开
        qo.setIsPublic(true);
        // 最后更新的时间升序排序
        qo.setOrderBy("t.lastUpdateTime asc");
        model.addAttribute("pageInfo", travelService.query(qo));
        return "/travels/releaseList";
    }

    /**
     * 根据游记的id去查询整条游记的信息
     */
    @RequestMapping("/getById")
    @ResponseBody
    public TravelContent getContentById(Long id) {
        return travelContentService.getById(id);
    }

    /**
     * 改变游记的状态 -> 发布/拒绝
     */
    @RequestMapping("/changeState")
    @ResponseBody
    public JsonResult changeState(Long id,Integer state){
        travelService.changeState(id,state);
        return new JsonResult() ;
    }
}
